---
title: "Descriptive Statistics, Graphics for Protest Image Paper"
output: pdf_document
---

# Setup
```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
#knitr::opts_knit$set(root.dir ='/Users/Zack/Dropbox/Twitter_ViolenceImages_postAPSR')
#library(knitr)
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=80),tidy=TRUE)

library(ggplot2)
library(dplyr)
library(reshape)
```

Load data.

Note that some NA values will be created for days when the denominator is 0, even if the numerator is also 0.  
```{r}
#setwd('<path/to/Replication/>')
cityday <- read.csv('./Data/02_processedData/e_DonghyeonAlexmerged_cityday_UsersAndMissingDays.csv', stringsAsFactors=FALSE)

cityday$country <- cityday$place.country_code
names(cityday) <- gsub('binary_', '', names(cityday))


cityday$kidFaces <- cityday$faces0_2_Sum + cityday$faces3_9_Sum
cityday$percKid <- cityday$kidFaces/cityday$totalFaces_Sum

cityday$percBlack <- cityday$raceBlack_Sum/cityday$totalFaces_Sum
cityday$percEastEasian <- cityday$raceEastAsian_Sum/cityday$totalFaces_Sum
cityday$percIndian <- cityday$raceIndian_Sum/cityday$totalFaces_Sum
cityday$percLatino <- cityday$raceLatino_Sum/cityday$totalFaces_Sum
cityday$percMiddleEast <- cityday$raceMiddleEast_Sum/cityday$totalFaces_Sum
cityday$percSEAsian <- cityday$raceSEAsian_Sum/cityday$totalFaces_Sum
cityday$percWhite <- cityday$raceWhite_Sum/cityday$totalFaces_Sum
cityday$percEastAsian <- cityday$raceEastAsian_Sum/cityday$totalFaces_Sum

cityday$percFemale <- cityday$facesFemale_Sum/cityday$totalFaces_Sum
cityday$percMale <- 1-cityday$percFemale


#cityday$percChildrenPhotos <- cityday$children/cityday$tweets.y 
#cityday$percWomen<- cityday$genderFemale_Mean/cityday$faces_Mean
names(cityday)[names(cityday) == 'tweets'] <- 'Protest Image'
```

Reshape to get long format.

```{r}
variables <- c('totalFaces_Sum', 'totalFaces_log', 'kidFaces', 'percKid', 'entropyGender_byDay', 'entropyRace_byDay', 'entropyAge_byDay', 'protest_result.protester_violence', 'protest_result.state_violence', 'police', 'fire', 'photo','group_20',  'faces_Mean', 'group_100',  'Protest Image', 'protester_violence', 'state_violence', 'percBlack', 'percEastAsian', 'percIndian', 'percLatino', 'percMiddleEast', 'percSEAsian', 'percWhite', 'percEastAsian', 'percFemale')
long <- melt(cityday, id.vars=c('country', 'day'), measure.vars=variables, variable_name='Measure')
```

# Plots

## Make Function
```{r}
makePlot <- function(data, country, variables, fileout, keep, labels, ylab, log=FALSE){
  these <- keep
  long <- melt(data, id.vars=c('country', 'day'), measure.vars=these, variable_name='Measure')
  temp <- long[long$country %in% country,]
  
  temp$value[is.nan(temp$value)==TRUE] <- 0  # Any NA value, replace with 0

  if(length(country)==1 & log==FALSE){
    ggplotsucks <-ggplot(temp, aes(x=as.Date(day), y=value)) + geom_line(aes(linetype=Measure)) + labs(x='', y=ylab) + scale_linetype(labels=labels)  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 90, hjust = 1, size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), axis.text.y=element_text(size=16), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + ylim(0, ceiling(max(temp$value*1.1, na.rm=TRUE)))
  }
  
  if(length(country)>1 & log==FALSE){
    ggplotsucks <-ggplot(temp, aes(x=as.Date(day), y=value)) + geom_line(aes(linetype=country)) + labs(x='', y=ylab) + scale_linetype(labels=labels)  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 90, hjust = 1, size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), axis.text.y=element_text(size=16), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + ylim(0, ceiling(max(temp$value*1.1, na.rm=TRUE)))
  }
  

    if(length(country)==1 & log==TRUE){
    ggplotsucks <-ggplot(temp, aes(x=as.Date(day), y=value)) + geom_line(aes(linetype=Measure)) + labs(x='', y=ylab) + scale_linetype(labels=labels)  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + scale_y_log10() + theme(axis.text.x = element_text(angle = 90, hjust = 1, size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), axis.text.y=element_text(size=16), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + ylim(0, ceiling(max(temp$value*1.1, na.rm=TRUE)))
  }
  
  if(length(country)>1 & log==TRUE){
    ggplotsucks <-ggplot(temp, aes(x=as.Date(day), y=value)) + geom_line(aes(linetype=country)) + labs(x='', y=ylab) + scale_linetype(labels=labels)  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + scale_y_log10() + theme(axis.text.x = element_text(angle = 90, hjust = 1, size=16), panel.grid.major=element_line(colour="grey95"), panel.grid.minor=element_blank(), panel.background=element_blank(), panel.border=element_rect(colour="black", fill=NA) + ylim(0, ceiling(max(temp$value*1.1, na.rm=TRUE))))
  }
  
    
  #pdf(fileout)
  jpeg(fileout,width=6, height=6, units='in', res=300)
  par(mar=c(5, 5, 1, 1))
  print(ggplotsucks)
  dev.off()
}
```


# New DV, More Readable, Main Countries

## Big Function
```{r}
countries <- unique(cityday$country)

for(i in 1:length(countries)){
  print(paste0('On ', countries[i]))
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i],'_totalFaces.jpg'), keep=c('totalFaces_Sum'), labels=c('Protest Size'), ylab='Protesters', log=TRUE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i],'_totalFaces_nolog.jpg'), keep=c('totalFaces_Sum'), labels=c('Protest Size'), ylab='Protesters', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_nfacesTweets.jpg'), keep=c('totalFaces_Sum', 'Protest Image'), labels=c('Protest Size', 'Protest Images'), ylab='Value', log=TRUE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_policeSGroup.jpg'), keep=c('police', 'group_20'), labels=c('Police', 'Small Group'), ylab='Count Tweets', log=TRUE)

      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_GenderRaceViolence.jpg'), keep=c('entropyGender_byDay', 'entropyRace_byDay', 'protest_result.protester_violence'), labels=c('Gender Diversity', 'Racial Diversity', 'Perc. Prot. Viol.'), ylab='Value', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_GenderPerc.jpg'), keep=c('percFemale'), labels=c('Pct. Female'), ylab='', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_ChildrenPerc.jpg'), keep=c('percKid'), labels=c('Pct. Photos w/ Children'), ylab='', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_GenderEntropy.jpg'), keep=c('entropyGender_byDay'), labels=c('Gender Diversity'), ylab='', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_RaceEntropy.jpg'), keep=c('entropyRace_byDay'), labels=c('Racial Diversity'), ylab='', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_GenderRaceEntropy.jpg'), keep=c('entropyGender_byDay', 'entropyRace_byDay'), labels=c('Gender Diversity', 'Racial Diversity'), ylab='Value', log=FALSE)

      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_PerceivedViolence_Protester.jpg'), keep=c('protest_result.protester_violence'), labels=c('Perc. Prtstr. Viol.'), ylab='', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_PerceivedViolence_State.jpg'), keep=c('protest_result.state_violence'), labels=c('Perc. Stt. Viol.'), ylab='', log=FALSE)
      
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_protStateViolence.jpg'), keep=c('protest_result.state_violence', 'protest_result.protester_violence'), labels=c('Perc. Stt Violence', 'Perc. Prtstr Violence'), ylab='Avg. Estimate', log=FALSE)
      
      # Below are all race variables
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percBlack.jpg'), keep=c('percBlack'), labels=c('Pct. Protesters Black'), ylab='', log=FALSE)
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percEastAsian.jpg'), keep=c('percEastAsian'), labels=c('Pct. Protesters East Asian'), ylab='', log=FALSE)
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percIndian.jpg'), keep=c('percIndian'), labels=c('Pct. Protesters South Asian'), ylab='', log=FALSE)
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percLatino.jpg'), keep=c('percLatino'), labels=c('Pct. Protesters Latinx'), ylab='', log=FALSE)
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percMiddleEast.jpg'), keep=c('percMiddleEast'), labels=c('Pct. Protesters Middle Easter'), ylab='', log=FALSE)
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percSEAsian.jpg'), keep=c('percSEAsian'), labels=c('Pct. Protesters SE Asian'), ylab='', log=FALSE)
      makePlot(data=cityday, country=countries[i], variables=variables, paste0('./Figures/', countries[i], '_percWhite.jpg'), keep=c('percWhite'), labels=c('Pct. Protesters White'), ylab='', log=FALSE)
}
```


# Seoul, South Korea Time Series

## Protest Size
```{r}
dates <- c('2016-10-29', '2016-11-12', '2016-12-03', '2017-01-07', '2017-03-11')
events <- c('First', 'Second Major \n Sat.', 'Largest Reported', 'Ferry Sinking', 'Geun-hye \n Removed')

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='totalFaces_Sum', variable_name='Measure')
temp <- long[long$city_use == 'Seoul',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(totalFaces_Sum=sum(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/fg2b.jpg', width=5, height=5, units="in", res=300)
ggplot(temp, aes(x=day, y=totalFaces_Sum)) + geom_line() + labs(x='', y='Protesters') + scale_linetype(labels='Protest Size')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(80,60,80,62,75), label=events, size=4, hjust=c(rep(-0.05,4),1.05)) + ylim(0,80)
dev.off()
```

Make another plot w/ dashed lines for Saturdays.

```{r}
dates <- seq.Date(as.Date('2016-10-20'), as.Date('2017-03-16'), by=7)

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='totalFaces_Sum', variable_name='Measure')
temp <- long[long$city_use == 'Seoul',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(totalFaces_Sum=sum(value)))


temp$day <- as.Date(temp$day)
temp <- temp[order(temp$day),]


dates2 <- c('2016-10-29', '2016-11-12', '2016-12-03', '2017-01-07', '2017-03-11')
events <- c('First', 'Second Major \n Sat.', 'Largest Reported', 'Ferry Sinking', 'Geun-hye \n Removed')

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))
xlines2 <- unlist(lapply(dates2, function(x) which(sort(unique(temp$day)) == x)))



jpeg('./Figures/fg2b_vDashes.jpeg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=totalFaces_Sum)) + geom_line() + labs(x='', y='Number of Protesters') + scale_linetype(labels='Protest Size')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines]+2)), linetype='dotted') + ylim(0,80) + annotate('text', x=as.Date(temp$day[xlines2]+1), y=c(80,70,80,72,75), label=events, hjust=c(rep(-0.05,4),1.05), size=4)
dev.off()
```

## State Violence
```{r}
dates <- c('2016-10-29', '2016-11-12', '2016-12-03', '2017-01-07', '2017-03-11')
events <- c('First', 'Second Major \n Sat.', 'Largest Reported', 'Ferry Sinking', 'Geun-hye \n Removed')

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='protest_result.state_violence', variable_name='Measure')
temp <- long[long$city_use == 'Seoul',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(stateViolence=mean(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/Seoul_stateviolence_annotated.jpg', width=5, height=5, units="in", res=300)
ggplot(temp, aes(x=day, y=stateViolence)) + geom_line() + labs(x='', y='State Violence') + scale_linetype(labels='State Violence')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(1,.70,1,.72,.85), label=events, size=4, hjust=c(rep(-0.05,4),1.05)) + ylim(0,1)
dev.off()
```



# Barcelona, Spain Time Series
## Protest Size
```{r}
dates <- c('2017-09-11', '2017-10-01', '2017-10-03', '2017-10-29')
events <- c('Annual \n March', 'Referendum', 'General Strike', 'Pro-unity Rally')

# 3 names for Barcleona, standardize
cityday$city_use <- gsub('Barcellona', 'Barcelona', cityday$city_use)
cityday$city_use <- gsub('Barcelone', 'Barcelona', cityday$city_use)


long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='totalFaces_Sum', variable_name='Measure')
temp <- long[long$city_use == 'Barcelona',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(totalFaces_Sum=sum(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)
temp <- temp[temp$day >= '2017-09-01',]

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/fg2a.jpg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=totalFaces_Sum)) + geom_line() + labs(x='', y='Protesters') + scale_linetype(labels='Protest Size')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(210, 400, 450, 365), label=events, hjust=c(-.05, 1.05, -.05, -.05), size=4) + ylim(0,500)
dev.off()
```

## State Violence
```{r}
dates <- c('2017-09-11', '2017-10-01', '2017-10-03', '2017-10-29')
events <- c('Annual \n March', 'Referendum', 'General Strike', 'Pro-unity Rally')

# 3 names for Barcleona, standardize
cityday$city_use <- gsub('Barcellona', 'Barcelona', cityday$city_use)
cityday$city_use <- gsub('Barcelone', 'Barcelona', cityday$city_use)

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='protest_result.state_violence', variable_name='Measure')
temp <- long[long$city_use == 'Barcelona',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(stateViolence=mean(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)

temp <- temp[temp$day >= '2017-09-01',]

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/Barcelona_stateviolence_annotated.jpg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=stateViolence)) + geom_line() + labs(x='', y='State Violence') + scale_linetype(labels='State Violence')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(.9, .75, .9, .75), label=events, hjust=c(-.05, 1.05, -.05, -.05), size=4) + ylim(0,1)
dev.off()
```

# Caracas Time Series
## Protest Size
```{r}
dates <- c('2014-11-02', '2015-01-01', '2015-01-09', '2015-01-24')
events <- c('Hydrobolivar Protests', 'Altamira Students', 'National\nGuard\nClash', 'Empty\nPots')

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='totalFaces_Sum', variable_name='Measure')
temp <- long[long$city_use == 'Caracas',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(totalFaces_Sum=sum(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)

# Keep just first period
temp <- temp[temp$day <= '2017-01-01',]

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/fg2c.jpg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=totalFaces_Sum)) + geom_line() + labs(x='', y='Protesters') + scale_linetype(labels='Protest Size')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(210, 400, 450, 400), label=events, hjust=c(-.05, 1.05, -.05, -.05), size=4) + ylim(0,500)
dev.off()
```

## State Violence
```{r}
dates <- c('2014-11-02', '2015-01-01', '2015-01-09', '2015-01-24')
events <- c('Hydrobolivar Protests', 'Altamira Students', 'National\nGuard\nClash', 'Empty\nPots')

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='protest_result.state_violence', variable_name='Measure')
temp <- long[long$city_use == 'Caracas',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(stateViolence=mean(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)

# Keep just first period
temp <- temp[temp$day <= '2017-01-01',]

xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/Caracas_stateviolence_annotated.jpg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=stateViolence)) + geom_line() + labs(x='', y='State Violence') + scale_linetype(labels='State Vioelnce')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 45, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(.9, .75, .9, .75), label=events, hjust=c(-.05, 1.05, -.05, -.05), size=4) + ylim(0,1)
dev.off()
```


# Hong Kong Time Series
## Protest Size
```{r}
dates <- c('2014-09-29', '2014-11-10', '2014-11-24', '2015-01-24')
events <- c('Occupy Central\nStarts', 'Protesting\nArrests', 'Clearance\nBegins')

long <- melt(cityday, id.vars=c('city_use', 'day'), measure.vars='totalFaces_Sum', variable_name='Measure')
temp <- long[long$city_use == 'Central',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(totalFaces_Sum=sum(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)


xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/fg2d.jpg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=totalFaces_Sum)) + geom_line() + labs(x='', y='Protesters') + scale_linetype(labels='Protest Size')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 75, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(21, 40, 45), label=events, hjust=c(-.05, 1.05, -.05), size=4) + ylim(0,50)
dev.off()
```

## State Violence
```{r}
dates <- c('2014-09-29', '2014-11-10', '2014-11-24', '2015-01-24')
events <- c('Occupy Central\nStarts', 'Protesting\nArrests', 'Clearance\nBegins')

long <- melt(cityday2, id.vars=c('city_use', 'day'), measure.vars='protest_result.state_violence', variable_name='Measure')
temp <- long[long$city_use == 'Central',]

temp <- data.frame(temp %>% group_by(day) %>% summarize(stateViolence=mean(value)))
temp <- temp[order(temp$day),]

temp$day <- as.Date(temp$day)


xlines <- unlist(lapply(dates, function(x) which(sort(unique(temp$day)) == x)))

jpeg('./Figures/HongKong_stateviolence_annotated.jpg', width=5, height=5, units='in', res=300)
ggplot(temp, aes(x=day, y=stateViolence)) + geom_line() + labs(x='', y='State Violence') + scale_linetype(labels='State Violence')  +  scale_x_date(date_labels = "%m.%d.%Y", date_breaks = "3 weeks") + theme(axis.text.x = element_text(angle = 75, vjust = .5, size=16), axis.text.y=element_text(size=16), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_rect(fill="white"), panel.border=element_rect(colour="black", fill=NA), legend.text=element_text(size=16), legend.title=element_text(size=16), axis.title.y=element_text(size=16)) + geom_vline(xintercept=as.numeric(as.Date(temp$day[xlines])), linetype='dotted') + annotate('text', x=as.Date(temp$day[xlines]), y=c(.9, .75, .9), label=events, hjust=c(-.05, 1.05, -.05), size=4) + ylim(0,1)
dev.off()
```



